//
//  imageViewController.m
//  movie
//
//  Created by 白浩洋 on 2018/4/3.
//  Copyright © 2018年 白浩洋. All rights reserved.
//

#import "imageViewController.h"
#import "MLBasePageViewController.h"
#import "imageDetailViewController.h"
#import "HotSearchCollectionViewCell.h"
#import "imgDetailViewController.h"
@interface imageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)NSMutableArray  *titlrDataSource;
@end
@implementation imageViewController
{
    NSInteger index;
    BOOL isdown;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addCustomTitleWithTitle:@"图片"];
    isdown =NO;
    index = 0;
    [self collectionView];
    [self requestDate];
}
- (void)requestDate{
    if(self.type == 1){
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"jiepai"];
    bquery.limit = 12;
    bquery.skip = index * 12;
    [bquery orderByDescending:@"id"];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if(error){
            NSLog(@"%@",error);
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
        }else{
            if(array.count > 0 && isdown){
                [self.dataSource removeAllObjects];
            }
            for (BmobObject *obj in array) {
                NSLog(@"%@",obj);
                NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
                [dic setObject:obj.objectId forKey:@"id"];
                [dic setObject:[obj objectForKey:@"detailimg"] forKey:@"detailimg"];
                [dic setObject:[obj objectForKey:@"covimg"] forKey:@"covimg"];
                [dic setObject:[obj objectForKey:@"yuedu"] forKey:@"yuedu"];
                [dic setObject:[obj objectForKey:@"shoujia"] forKey:@"shoujia"];
                [self.dataSource addObject:dic];
            }
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
            [self.collectionView reloadData];
        }
    }];
    }else {
        BmobQuery *bquery = [BmobQuery queryWithClassName:@"meizitu"];
        bquery.limit = 12;
        bquery.skip = index * 12;
        [bquery orderByDescending:@"id"];
        [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            if(error){
                NSLog(@"%@",error);
                [self.collectionView.mj_header endRefreshing];
                [self.collectionView.mj_footer endRefreshing];
            }else{
                if(array.count > 0 && isdown){
                    [self.dataSource removeAllObjects];
                }
                for (BmobObject *obj in array) {
                    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
                    [dic setObject:obj.objectId forKey:@"id"];
                    [dic setObject:[obj objectForKey:@"images"] forKey:@"detailimg"];
                    [dic setObject:[obj objectForKey:@"covimg"] forKey:@"covimg"];
                    [dic setObject:[obj objectForKey:@"yuedu"] forKey:@"yuedu"];
                    [dic setObject:[obj objectForKey:@"shoujia"] forKey:@"shoujia"];
                    [self.dataSource addObject:dic];
                }
                [self.collectionView.mj_header endRefreshing];
                [self.collectionView.mj_footer endRefreshing];
                [self.collectionView reloadData];
            }
        }];
    }
}
- (NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (NSMutableArray *)titlrDataSource{
    if(!_titlrDataSource){
        _titlrDataSource = [[NSMutableArray alloc]initWithArray:@[@"街拍", @"热点", @"会员"]];
    }
    return _titlrDataSource;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger i = [self.dataSource[indexPath.row][@"shoujia"] intValue];
    NSInteger j = [self.dataSource[indexPath.row][@"yuedu"] intValue];
    j+=3;
    NSString *tabName =@"";
    if(self.type == 1){
        tabName = @"jiepai";
    }else{
        tabName = @"meizitu";
    }
    [NBATools setyuedu:[NSString stringWithFormat:@"%ld",(long)j] WithID:self.dataSource[indexPath.row][@"id"] withTabName:tabName];
    NSInteger totalStar = [NBATools getstar];
    if(totalStar - i > 0){
        [NBATools setstar:totalStar - i];
        [NBATools setstarOnline:(totalStar - i)];
        imgDetailViewController *IMG = [[imgDetailViewController alloc]init];
        IMG.textss = self.dataSource[indexPath.row][@"detailimg"];
        IMG.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:IMG animated:YES];
    }else{
        UIAlertController *alert = [NBATools showAlert];
        [self presentViewController:alert animated:YES completion:nil];
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HotSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotSearchCell1" forIndexPath:indexPath];
    if(!cell){
        cell = [[HotSearchCollectionViewCell alloc] init];
        cell.readLab.textColor = [UIColor colorWithHexString:@"@#666666"];
        cell.readLab.font = [UIFont systemFontOfSize:PXChange(10)];
        cell.cellLabel.textColor = [UIColor colorWithHexString:@"@#666666"];
        cell.cellLabel.font = [UIFont systemFontOfSize:PXChange(10)];
    }
    cell.movieName.hidden = YES;
    NSDictionary *model=self.dataSource[indexPath.item];
    NSString *movieImage=model[@"covimg"];
    [cell.movieImage sd_setImageWithURL:[NSURL URLWithString:movieImage] placeholderImage:[UIImage imageNamed:@"logo"]];
    cell.movieName.text=model[@"des"];
    cell.cellLabel.text = [NSString stringWithFormat:@"  售价:%@星",model[@"shoujia"]];
    cell.readLab.text = [NSString stringWithFormat:@"%@已阅  ",model[@"yuedu"]];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(ScreenWidth / 3 - PXChange(30), ((ScreenWidth / 3 + PXChange(20))/4.0f)*4.0f+PXChange(60));
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10,10,0,10);
}
-(UICollectionViewFlowLayout *)flowLayout{
    if(!_flowLayout){
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //设置滑动方向
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _flowLayout.minimumLineSpacing = PXChange(20);
    }
    return _flowLayout;
}
-(UICollectionView *)collectionView{
    if(!_collectionView){
        _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.width,self.view.height - 84 - PXChange(88)) collectionViewLayout:self.flowLayout];
        _collectionView.delegate=self;
        _collectionView.dataSource=self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.contentSize=CGSizeMake(ScreenWidth/2.0f, self.view.height - 64 - PXChange(88));
        [_collectionView registerNib:[UINib nibWithNibName:@"HotSearchCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"hotSearchCell1"];
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            index = 0;
            isdown = YES;
            [self requestDate];
        }];
        _collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            index ++;
            isdown = NO;
            [self requestDate];
        }];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

@end
